
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>Examples &#8212; pwlf 1.1.6 documentation</title>
    <link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/language_data.js"></script>
    <link rel="shortcut icon" href="_static/favicon.ico"/>
    <link rel="author" title="About these documents" href="about.html" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="pwlf package contents" href="pwlf.html" />
    <link rel="prev" title="How it works" href="how_it_works.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <div class="section" id="examples">
<h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
<p>All of these examples will use the following data and imports.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">pwlf</span>

<span class="c1"># your data</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">9.69801700e-03</span><span class="p">,</span> <span class="mf">2.94350340e-02</span><span class="p">,</span>
              <span class="mf">4.39052750e-02</span><span class="p">,</span> <span class="mf">5.45343950e-02</span><span class="p">,</span> <span class="mf">6.74104940e-02</span><span class="p">,</span>
              <span class="mf">8.34831790e-02</span><span class="p">,</span> <span class="mf">1.02580042e-01</span><span class="p">,</span> <span class="mf">1.22767939e-01</span><span class="p">,</span>
              <span class="mf">1.42172312e-01</span><span class="p">,</span> <span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">8.58600000e-06</span><span class="p">,</span>
              <span class="mf">8.31543400e-03</span><span class="p">,</span> <span class="mf">2.34184100e-02</span><span class="p">,</span> <span class="mf">3.39709150e-02</span><span class="p">,</span>
              <span class="mf">4.03581990e-02</span><span class="p">,</span> <span class="mf">4.53545600e-02</span><span class="p">,</span> <span class="mf">5.02345260e-02</span><span class="p">,</span>
              <span class="mf">5.55253360e-02</span><span class="p">,</span> <span class="mf">6.14750770e-02</span><span class="p">,</span> <span class="mf">6.82125120e-02</span><span class="p">,</span>
              <span class="mf">7.55892510e-02</span><span class="p">,</span> <span class="mf">8.38356810e-02</span><span class="p">,</span> <span class="mf">9.26413070e-02</span><span class="p">,</span>
              <span class="mf">1.02039790e-01</span><span class="p">,</span> <span class="mf">1.11688258e-01</span><span class="p">,</span> <span class="mf">1.21390666e-01</span><span class="p">,</span>
              <span class="mf">1.31196948e-01</span><span class="p">,</span> <span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">1.56706510e-02</span><span class="p">,</span>
              <span class="mf">3.54628780e-02</span><span class="p">,</span> <span class="mf">4.63739040e-02</span><span class="p">,</span> <span class="mf">5.61442590e-02</span><span class="p">,</span>
              <span class="mf">6.78542550e-02</span><span class="p">,</span> <span class="mf">8.16388310e-02</span><span class="p">,</span> <span class="mf">9.77756110e-02</span><span class="p">,</span>
              <span class="mf">1.16531753e-01</span><span class="p">,</span> <span class="mf">1.37038283e-01</span><span class="p">,</span> <span class="mf">0.00000000e+00</span><span class="p">,</span>
              <span class="mf">1.16951050e-02</span><span class="p">,</span> <span class="mf">3.12089850e-02</span><span class="p">,</span> <span class="mf">4.41776550e-02</span><span class="p">,</span>
              <span class="mf">5.42877590e-02</span><span class="p">,</span> <span class="mf">6.63321350e-02</span><span class="p">,</span> <span class="mf">8.07655920e-02</span><span class="p">,</span>
              <span class="mf">9.70363280e-02</span><span class="p">,</span> <span class="mf">1.15706975e-01</span><span class="p">,</span> <span class="mf">1.36687642e-01</span><span class="p">,</span>
              <span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">1.50144640e-02</span><span class="p">,</span> <span class="mf">3.44519970e-02</span><span class="p">,</span>
              <span class="mf">4.55907760e-02</span><span class="p">,</span> <span class="mf">5.59556700e-02</span><span class="p">,</span> <span class="mf">6.88450940e-02</span><span class="p">,</span>
              <span class="mf">8.41374060e-02</span><span class="p">,</span> <span class="mf">1.01254006e-01</span><span class="p">,</span> <span class="mf">1.20605073e-01</span><span class="p">,</span>
              <span class="mf">1.41881288e-01</span><span class="p">,</span> <span class="mf">1.62618058e-01</span><span class="p">])</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">8.82678000e-03</span><span class="p">,</span> <span class="mf">3.25615100e-02</span><span class="p">,</span>
              <span class="mf">5.66106800e-02</span><span class="p">,</span> <span class="mf">7.95549800e-02</span><span class="p">,</span> <span class="mf">1.00936330e-01</span><span class="p">,</span>
              <span class="mf">1.20351520e-01</span><span class="p">,</span> <span class="mf">1.37442010e-01</span><span class="p">,</span> <span class="mf">1.51858250e-01</span><span class="p">,</span>
              <span class="mf">1.64433570e-01</span><span class="p">,</span> <span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.12600000e-05</span><span class="p">,</span>
              <span class="mf">7.03872000e-03</span><span class="p">,</span> <span class="mf">1.85494500e-02</span><span class="p">,</span> <span class="mf">3.00926700e-02</span><span class="p">,</span>
              <span class="mf">4.17617000e-02</span><span class="p">,</span> <span class="mf">5.37279600e-02</span><span class="p">,</span> <span class="mf">6.54941000e-02</span><span class="p">,</span>
              <span class="mf">7.68092100e-02</span><span class="p">,</span> <span class="mf">8.76596300e-02</span><span class="p">,</span> <span class="mf">9.80525800e-02</span><span class="p">,</span>
              <span class="mf">1.07961810e-01</span><span class="p">,</span> <span class="mf">1.17305210e-01</span><span class="p">,</span> <span class="mf">1.26063930e-01</span><span class="p">,</span>
              <span class="mf">1.34180360e-01</span><span class="p">,</span> <span class="mf">1.41725010e-01</span><span class="p">,</span> <span class="mf">1.48629710e-01</span><span class="p">,</span>
              <span class="mf">1.55374770e-01</span><span class="p">,</span> <span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">1.65610200e-02</span><span class="p">,</span>
              <span class="mf">3.91016100e-02</span><span class="p">,</span> <span class="mf">6.18679400e-02</span><span class="p">,</span> <span class="mf">8.30997400e-02</span><span class="p">,</span>
              <span class="mf">1.02132890e-01</span><span class="p">,</span> <span class="mf">1.19011260e-01</span><span class="p">,</span> <span class="mf">1.34620080e-01</span><span class="p">,</span>
              <span class="mf">1.49429370e-01</span><span class="p">,</span> <span class="mf">1.63539960e-01</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.00000000e+00</span><span class="p">,</span>
              <span class="mf">1.01980300e-02</span><span class="p">,</span> <span class="mf">3.28642800e-02</span><span class="p">,</span> <span class="mf">5.59461900e-02</span><span class="p">,</span>
              <span class="mf">7.81388400e-02</span><span class="p">,</span> <span class="mf">9.84458400e-02</span><span class="p">,</span> <span class="mf">1.16270210e-01</span><span class="p">,</span>
              <span class="mf">1.31279040e-01</span><span class="p">,</span> <span class="mf">1.45437090e-01</span><span class="p">,</span> <span class="mf">1.59627540e-01</span><span class="p">,</span>
              <span class="mf">0.00000000e+00</span><span class="p">,</span> <span class="mf">1.63404300e-02</span><span class="p">,</span> <span class="mf">4.00086000e-02</span><span class="p">,</span>
              <span class="mf">6.34390200e-02</span><span class="p">,</span> <span class="mf">8.51085900e-02</span><span class="p">,</span> <span class="mf">1.04787860e-01</span><span class="p">,</span>
              <span class="mf">1.22120350e-01</span><span class="p">,</span> <span class="mf">1.36931660e-01</span><span class="p">,</span> <span class="mf">1.50958760e-01</span><span class="p">,</span>
              <span class="mf">1.65299640e-01</span><span class="p">,</span> <span class="mf">1.79942720e-01</span><span class="p">])</span>
</pre></div>
</div>
<ol class="arabic simple">
<li><p><a class="reference external" href="#fit-with-known-breakpoint-locations">fit with known breakpoint
locations</a></p></li>
<li><p><a class="reference external" href="#fit-for-specified-number-of-line-segments">fit for specified number of line
segments</a></p></li>
<li><p><a class="reference external" href="#fitfast-for-specified-number-of-line-segments">fitfast for specified number of line
segments</a></p></li>
<li><p><a class="reference external" href="#force-a-fit-through-data-points">force a fit through data
points</a></p></li>
<li><p><a class="reference external" href="#use-custom-optimization-routine">use custom optimization
routine</a></p></li>
<li><p><a class="reference external" href="#pass-differential-evolution-keywords">pass differential evolution
keywords</a></p></li>
<li><p><a class="reference external" href="#find-the-best-number-of-line-segments">find the best number of line
segments</a></p></li>
<li><p><a class="reference external" href="#model-persistence">model persistence</a></p></li>
<li><p><a class="reference external" href="#bad-fits-when-you-have-more-unknowns-than-data">bad fits when you have more unknowns than
data</a></p></li>
<li><p><a class="reference external" href="#fit-with-a-breakpoint-guess">fit with a breakpoint guess</a></p></li>
<li><p><a class="reference external" href="#get-the-linear-regression-matrix">get the linear regression
matrix</a></p></li>
<li><p><a class="reference external" href="#use-of-tensorflow">use of TensorFlow</a></p></li>
<li><p><a class="reference external" href="#fit-constants-or-polynomials">fit constants or polynomials</a></p></li>
<li><p><a class="reference external" href="#specify-breakpoint-bounds">specify breakpoint bounds</a></p></li>
<li><p><a class="reference external" href="#non-linear-standard-errors-and-p-values">non-linear standard errors and
p-values</a></p></li>
<li><p><a class="reference external" href="#obtain-the-equations-of-fitted-pwlf">obtain the equations of fitted
pwlf</a></p></li>
</ol>
<div class="section" id="fit-with-known-breakpoint-locations">
<h2>fit with known breakpoint locations<a class="headerlink" href="#fit-with-known-breakpoint-locations" title="Permalink to this headline">¶</a></h2>
<p>You can perform a least squares fit if you know the breakpoint
locations.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># your desired line segment end locations</span>
<span class="n">x0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="mf">0.039</span><span class="p">,</span> <span class="mf">0.10</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">)])</span>

<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># fit the data with the specified break points</span>
<span class="c1"># (ie the x locations of where the line segments</span>
<span class="c1"># will terminate)</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_with_breaks</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id1">
<img alt="fit with known breakpoint locations" src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/fit_breaks.png" />
<p class="caption"><span class="caption-text">fit with known breakpoint locations</span><a class="headerlink" href="#id1" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="fit-for-specified-number-of-line-segments">
<h2>fit for specified number of line segments<a class="headerlink" href="#fit-for-specified-number-of-line-segments" title="Permalink to this headline">¶</a></h2>
<p>Use a global optimization to find the breakpoint locations that minimize
the sum of squares error. This uses <a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html">Differential
Evolution</a>
from scipy.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># fit the data for four line segments</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id2">
<img alt="fit for specified number of line segments" src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/numberoflines.png" />
<p class="caption"><span class="caption-text">fit for specified number of line segments</span><a class="headerlink" href="#id2" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="fitfast-for-specified-number-of-line-segments">
<h2>fitfast for specified number of line segments<a class="headerlink" href="#fitfast-for-specified-number-of-line-segments" title="Permalink to this headline">¶</a></h2>
<p>This performs a fit for a specified number of line segments with a
multi-start gradient based optimization. This should be faster than
<a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html">Differential
Evolution</a>
for a small number of starting points.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># fit the data for four line segments</span>
<span class="c1"># this performs 3 multi-start optimizations</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fitfast</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">pop</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id3">
<img alt="fitfast for specified number of line segments" src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/fitfast.png" />
<p class="caption"><span class="caption-text">fitfast for specified number of line segments</span><a class="headerlink" href="#id3" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="force-a-fit-through-data-points">
<h2>force a fit through data points<a class="headerlink" href="#force-a-fit-through-data-points" title="Permalink to this headline">¶</a></h2>
<p>Sometimes it’s necessary to force the piecewise continuous model through
a particular data point, or a set of data points. The following example
finds the best 4 line segments that go through two data points.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">myPWLF</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># fit the function with four line segments</span>
<span class="c1"># force the function to go through the data points</span>
<span class="c1"># (0.0, 0.0) and (0.19, 0.16)</span>
<span class="c1"># where the data points are of the form (x, y)</span>
<span class="n">x_c</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.19</span><span class="p">]</span>
<span class="n">y_c</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">]</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">myPWLF</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">x_c</span><span class="p">,</span> <span class="n">y_c</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="mf">0.19</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">myPWLF</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id4">
<img alt="force a fit through data points" src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/force.png" />
<p class="caption"><span class="caption-text">force a fit through data points</span><a class="headerlink" href="#id4" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="use-custom-optimization-routine">
<h2>use custom optimization routine<a class="headerlink" href="#use-custom-optimization-routine" title="Permalink to this headline">¶</a></h2>
<p>You can use your favorite optimization routine to find the breakpoint
locations. The following example uses scipy’s
<a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html">minimize</a>
function.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">scipy.optimize</span> <span class="kn">import</span> <span class="n">minimize</span>
<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># initialize custom optimization</span>
<span class="n">number_of_line_segments</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">use_custom_opt</span><span class="p">(</span><span class="n">number_of_line_segments</span><span class="p">)</span>

<span class="c1"># i have number_of_line_segments - 1 number of variables</span>
<span class="c1"># let&#39;s guess the correct location of the two unknown variables</span>
<span class="c1"># (the program defaults to have end segments at x0= min(x)</span>
<span class="c1"># and xn=max(x)</span>
<span class="n">xGuess</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">number_of_line_segments</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">xGuess</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.02</span>
<span class="n">xGuess</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.10</span>

<span class="n">res</span> <span class="o">=</span> <span class="n">minimize</span><span class="p">(</span><span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_with_breaks_opt</span><span class="p">,</span> <span class="n">xGuess</span><span class="p">)</span>

<span class="c1"># set up the break point locations</span>
<span class="n">x0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">number_of_line_segments</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">x0</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x0</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x0</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">x</span>

<span class="c1"># calculate the parameters based on the optimal break point locations</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_with_breaks</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="pass-differential-evolution-keywords">
<h2>pass differential evolution keywords<a class="headerlink" href="#pass-differential-evolution-keywords" title="Permalink to this headline">¶</a></h2>
<p>You can pass keyword arguments from the <code class="docutils literal notranslate"><span class="pre">fit</span></code> function into scipy’s
<a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html">Differential
Evolution</a>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># fit the data for four line segments</span>
<span class="c1"># this sets DE to have an absolute tolerance of 0.1</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">atol</span><span class="o">=</span><span class="mf">0.1</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="find-the-best-number-of-line-segments">
<h2>find the best number of line segments<a class="headerlink" href="#find-the-best-number-of-line-segments" title="Permalink to this headline">¶</a></h2>
<p>This example uses EGO (bayesian optimization) and a penalty function to
find the best number of line segments. This will require careful use of
the penalty parameter <code class="docutils literal notranslate"><span class="pre">l</span></code>. Use this template to automatically find the
best number of line segments.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">GPyOpt.methods</span> <span class="kn">import</span> <span class="n">BayesianOptimization</span>
<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># define your objective function</span>


<span class="k">def</span> <span class="nf">my_obj</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="c1"># define some penalty parameter l</span>
    <span class="c1"># you&#39;ll have to arbitrarily pick this</span>
    <span class="c1"># it depends upon the noise in your data,</span>
    <span class="c1"># and the value of your sum of square of residuals</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="o">*</span><span class="mf">0.001</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
        <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">j</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">ssr</span> <span class="o">+</span> <span class="p">(</span><span class="n">l</span><span class="o">*</span><span class="n">j</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">f</span>


<span class="c1"># define the lower and upper bound for the number of line segments</span>
<span class="n">bounds</span> <span class="o">=</span> <span class="p">[{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;var_1&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;discrete&#39;</span><span class="p">,</span>
           <span class="s1">&#39;domain&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">40</span><span class="p">)}]</span>

<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">12121</span><span class="p">)</span>

<span class="n">myBopt</span> <span class="o">=</span> <span class="n">BayesianOptimization</span><span class="p">(</span><span class="n">my_obj</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="n">bounds</span><span class="p">,</span> <span class="n">model_type</span><span class="o">=</span><span class="s1">&#39;GP&#39;</span><span class="p">,</span>
                              <span class="n">initial_design_numdata</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
                              <span class="n">initial_design_type</span><span class="o">=</span><span class="s1">&#39;latin&#39;</span><span class="p">,</span>
                              <span class="n">exact_feval</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">verbosity</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                              <span class="n">verbosity_model</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">max_iter</span> <span class="o">=</span> <span class="mi">30</span>

<span class="c1"># perform the bayesian optimization to find the optimum number</span>
<span class="c1"># of line segments</span>
<span class="n">myBopt</span><span class="o">.</span><span class="n">run_optimization</span><span class="p">(</span><span class="n">max_iter</span><span class="o">=</span><span class="n">max_iter</span><span class="p">,</span> <span class="n">verbosity</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1"> </span><span class="se">\n</span><span class="s1"> Opt found </span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s1">&#39;Optimum number of line segments:&#39;</span><span class="p">,</span> <span class="n">myBopt</span><span class="o">.</span><span class="n">x_opt</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s1">&#39;Function value:&#39;</span><span class="p">,</span> <span class="n">myBopt</span><span class="o">.</span><span class="n">fx_opt</span><span class="p">)</span>
<span class="n">myBopt</span><span class="o">.</span><span class="n">plot_acquisition</span><span class="p">()</span>
<span class="n">myBopt</span><span class="o">.</span><span class="n">plot_convergence</span><span class="p">()</span>

<span class="c1"># perform the fit for the optimum</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">myBopt</span><span class="o">.</span><span class="n">x_opt</span><span class="p">)</span>
<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="model-persistence">
<h2>model persistence<a class="headerlink" href="#model-persistence" title="Permalink to this headline">¶</a></h2>
<p>You can save fitted models with pickle. Alternatively see
<a class="reference external" href="https://joblib.readthedocs.io/en/latest/">joblib</a>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># if you use Python 2.x you should import cPickle</span>
<span class="c1"># import cPickle as pickle</span>
<span class="c1"># if you use Python 3.x you can just use pickle</span>
<span class="kn">import</span> <span class="nn">pickle</span>

<span class="c1"># your desired line segment end locations</span>
<span class="n">x0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="mf">0.039</span><span class="p">,</span> <span class="mf">0.10</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">)])</span>

<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># fit the data with the specified break points</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_with_breaks</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span>

<span class="c1"># save the fitted model</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;my_fit.pkl&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">my_pwlf</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">pickle</span><span class="o">.</span><span class="n">HIGHEST_PROTOCOL</span><span class="p">)</span>

<span class="c1"># load the fitted model</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;my_fit.pkl&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="bad-fits-when-you-have-more-unknowns-than-data">
<h2>bad fits when you have more unknowns than data<a class="headerlink" href="#bad-fits-when-you-have-more-unknowns-than-data" title="Permalink to this headline">¶</a></h2>
<p>You can get very bad fits with pwlf when you have more unknowns than
data points. The following example will fit 99 line segments to the 59
data points. While this will result in an error of zero, the model will
have very weird predictions within the data. You should not fit more
unknowns than you have data with pwlf!</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">break_locations</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_with_breaks</span><span class="p">(</span><span class="n">break_locations</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id5">
<img alt="bad fits when you have more unknowns than data" src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/badfit.png" />
<p class="caption"><span class="caption-text">bad fits when you have more unknowns than data</span><a class="headerlink" href="#id5" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="fit-with-a-breakpoint-guess">
<h2>fit with a breakpoint guess<a class="headerlink" href="#fit-with-a-breakpoint-guess" title="Permalink to this headline">¶</a></h2>
<p>In this example we see two distinct linear regions, and we believe a
breakpoint occurs at 6.0. We’ll use the fit_guess() function to find the
best breakpoint location starting with this guess. These fits should be
much faster than the <code class="docutils literal notranslate"><span class="pre">fit</span></code> or <code class="docutils literal notranslate"><span class="pre">fitfast</span></code> function when you have a
reasonable idea where the breakpoints occur.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pwlf</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">4.</span><span class="p">,</span> <span class="mf">5.</span><span class="p">,</span> <span class="mf">6.</span><span class="p">,</span> <span class="mf">7.</span><span class="p">,</span> <span class="mf">8.</span><span class="p">])</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">11.</span><span class="p">,</span> <span class="mf">13.</span><span class="p">,</span> <span class="mf">16.</span><span class="p">,</span> <span class="mf">28.92</span><span class="p">,</span> <span class="mf">42.81</span><span class="p">])</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">breaks</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_guess</span><span class="p">([</span><span class="mf">6.0</span><span class="p">])</span>
</pre></div>
</div>
<p>Note specifying one breakpoint will result in two line segments. If we
wanted three line segments, we’ll have to specify two breakpoints.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">breaks</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_guess</span><span class="p">([</span><span class="mf">5.5</span><span class="p">,</span> <span class="mf">6.0</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="get-the-linear-regression-matrix">
<h2>get the linear regression matrix<a class="headerlink" href="#get-the-linear-regression-matrix" title="Permalink to this headline">¶</a></h2>
<p>In some cases it may be desirable to work with the linear regression
matrix directly. The following example grabs the linear regression
matrix <code class="docutils literal notranslate"><span class="pre">A</span></code> for a specific set of breakpoints. In this case we assume
that the breakpoints occur at each of the data points. Please see the
<a class="reference external" href="https://github.com/cjekel/piecewise_linear_fit_py/tree/master/paper">paper</a>
for details about the regression matrix <code class="docutils literal notranslate"><span class="pre">A</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pwlf</span>
<span class="c1"># select random seed for reproducibility</span>
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span>
<span class="c1"># generate sin wave data</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">ytrue</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="c1"># add noise to the data</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span> <span class="o">+</span> <span class="n">ytrue</span>

<span class="n">my_pwlf_en</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="c1"># copy the x data to use as break points</span>
<span class="n">breaks</span> <span class="o">=</span> <span class="n">my_pwlf_en</span><span class="o">.</span><span class="n">x_data</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="c1"># create the linear regression matrix A</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">my_pwlf_en</span><span class="o">.</span><span class="n">assemble_regression_matrix</span><span class="p">(</span><span class="n">breaks</span><span class="p">,</span> <span class="n">my_pwlf_en</span><span class="o">.</span><span class="n">x_data</span><span class="p">)</span>
</pre></div>
</div>
<p>We can perform fits that are more complicated than a least squares fit
when we have the regression matrix. The following uses the Elastic Net
regularizer to perform an interesting fit with the regression matrix.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">ElasticNetCV</span>
<span class="c1"># set up the elastic net</span>
<span class="n">en_model</span> <span class="o">=</span> <span class="n">ElasticNetCV</span><span class="p">(</span><span class="n">cv</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
                        <span class="n">l1_ratio</span><span class="o">=</span><span class="p">[</span><span class="o">.</span><span class="mi">1</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="o">.</span><span class="mi">7</span><span class="p">,</span> <span class="o">.</span><span class="mi">9</span><span class="p">,</span>
                                  <span class="o">.</span><span class="mi">95</span><span class="p">,</span> <span class="o">.</span><span class="mi">99</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
                        <span class="n">fit_intercept</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                        <span class="n">max_iter</span><span class="o">=</span><span class="mi">1000000</span><span class="p">,</span> <span class="n">n_jobs</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># fit the model using the elastic net</span>
<span class="n">en_model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">my_pwlf_en</span><span class="o">.</span><span class="n">y_data</span><span class="p">)</span>

<span class="c1"># predict from the elastic net parameters</span>
<span class="n">xhat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">min</span><span class="p">(),</span> <span class="n">x</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span> <span class="mi">1000</span><span class="p">)</span>
<span class="n">yhat_en</span> <span class="o">=</span> <span class="n">my_pwlf_en</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xhat</span><span class="p">,</span> <span class="n">breaks</span><span class="o">=</span><span class="n">breaks</span><span class="p">,</span>
                             <span class="n">beta</span><span class="o">=</span><span class="n">en_model</span><span class="o">.</span><span class="n">coef_</span><span class="p">)</span>
</pre></div>
</div>
<div class="figure align-default" id="id6">
<img alt="interesting elastic net fit" src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/sin_en_net_fit.png" />
<p class="caption"><span class="caption-text">interesting elastic net fit</span><a class="headerlink" href="#id6" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="use-of-tensorflow">
<h2>use of tensorflow<a class="headerlink" href="#use-of-tensorflow" title="Permalink to this headline">¶</a></h2>
<p>You’ll be able to use the <code class="docutils literal notranslate"><span class="pre">PiecewiseLinFitTF</span></code> class if you have
TensorFlow installed, which may offer performance improvements for
larger data sets over the original <code class="docutils literal notranslate"><span class="pre">PiecewiseLinFit</span></code> class. For
performance benchmarks see this blog
<a class="reference external" href="https://jekel.me/2019/Adding-tensorflow-to-pwlf/">post</a>.</p>
<p>The use of the TF class is nearly identical to the original class,
however note the following exceptions. <code class="docutils literal notranslate"><span class="pre">PiecewiseLinFitTF</span></code> does:</p>
<ul class="simple">
<li><p>not have a <code class="docutils literal notranslate"><span class="pre">lapack_driver</span></code> option</p></li>
<li><p>have an optional parameter <code class="docutils literal notranslate"><span class="pre">dtype</span></code>, so you can choose between the
float64 and float32 data types</p></li>
<li><p>have an optional parameter <code class="docutils literal notranslate"><span class="pre">fast</span></code> to switch between Cholesky
decomposition (default <code class="docutils literal notranslate"><span class="pre">fast=True</span></code>), and orthogonal decomposition
(<code class="docutils literal notranslate"><span class="pre">fast=False</span></code>)</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># your desired line segment end locations</span>
<span class="n">x0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="mf">0.039</span><span class="p">,</span> <span class="mf">0.10</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">)])</span>

<span class="c1"># initialize TF piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFitTF</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;float32)</span>

<span class="c1"># fit the data with the specified break points</span>
<span class="c1"># (ie the x locations of where the line segments</span>
<span class="c1"># will terminate)</span>
<span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_with_breaks</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="fit-constants-or-polynomials">
<h2>fit constants or polynomials<a class="headerlink" href="#fit-constants-or-polynomials" title="Permalink to this headline">¶</a></h2>
<p>You can use pwlf to fit segmented constant models, or piecewise
polynomials. The following example fits a segmented constant model,
piecewise linear, and a piecewise quadratic model to a sine wave.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># generate sin wave data</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># add noise to the data</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span> <span class="o">+</span> <span class="n">y</span>

<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="c1"># pwlf lets you fit continuous model for many degree polynomials</span>
<span class="c1"># degree=0 constant</span>
<span class="c1"># degree=1 linear (default)</span>
<span class="c1"># degree=2 quadratic</span>
<span class="n">my_pwlf_0</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">degree</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">my_pwlf_1</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">degree</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># default</span>
<span class="n">my_pwlf_2</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">degree</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

<span class="c1"># fit the data for four line segments</span>
<span class="n">res0</span> <span class="o">=</span> <span class="n">my_pwlf_0</span><span class="o">.</span><span class="n">fitfast</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">pop</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="n">res1</span> <span class="o">=</span> <span class="n">my_pwlf_1</span><span class="o">.</span><span class="n">fitfast</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">pop</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="n">res2</span> <span class="o">=</span> <span class="n">my_pwlf_2</span><span class="o">.</span><span class="n">fitfast</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">pop</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

<span class="c1"># predict for the determined points</span>
<span class="n">xHat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">yHat0</span> <span class="o">=</span> <span class="n">my_pwlf_0</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>
<span class="n">yHat1</span> <span class="o">=</span> <span class="n">my_pwlf_1</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>
<span class="n">yHat2</span> <span class="o">=</span> <span class="n">my_pwlf_2</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">xHat</span><span class="p">)</span>

<span class="c1"># plot the results</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Data&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat0</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;degree=0&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat1</span><span class="p">,</span> <span class="s1">&#39;--&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;degree=1&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xHat</span><span class="p">,</span> <span class="n">yHat2</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;degree=2&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id7">
<img alt="Example of multiple degree fits to a sine wave." src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/multi_degree.png" />
<p class="caption"><span class="caption-text">Example of multiple degree fits to a sine wave.</span><a class="headerlink" href="#id7" title="Permalink to this image">¶</a></p>
</div>
</div>
<div class="section" id="specify-breakpoint-bounds">
<h2>specify breakpoint bounds<a class="headerlink" href="#specify-breakpoint-bounds" title="Permalink to this headline">¶</a></h2>
<p>You may want extra control over the search space for feasible
breakpoints. One way to do this is to specify the bounds for each
breakpoint location.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># generate sin wave data</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># add noise to the data</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span> <span class="o">+</span> <span class="n">y</span>

<span class="c1"># initialize piecewise linear fit with your x and y data</span>
<span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c1"># define custom bounds for the interior break points</span>
<span class="n">n_segments</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">bounds</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_segments</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="c1"># first breakpoint</span>
<span class="n">bounds</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>  <span class="c1"># lower bound</span>
<span class="n">bounds</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mf">3.5</span>  <span class="c1"># upper bound</span>
<span class="c1"># second breakpoint</span>
<span class="n">bounds</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">3.0</span>  <span class="c1"># lower bound</span>
<span class="n">bounds</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mf">7.0</span>  <span class="c1"># upper bound</span>
<span class="c1"># third breakpoint</span>
<span class="n">bounds</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">6.0</span>  <span class="c1"># lower bound</span>
<span class="n">bounds</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mf">10.0</span>  <span class="c1"># upper bound</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">n_segments</span><span class="p">,</span> <span class="n">bounds</span><span class="o">=</span><span class="n">bounds</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="non-linear-standard-errors-and-p-values">
<h2>non-linear standard errors and p-values<a class="headerlink" href="#non-linear-standard-errors-and-p-values" title="Permalink to this headline">¶</a></h2>
<p>You can calculate non-linear standard errors using the Delta method.
This will calculate the standard errors of the piecewise linear
parameters (intercept + slopes) and the breakpoint locations!</p>
<p>First let us generate true piecewise linear data.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="c1"># generate a true piecewise linear data</span>
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">n_data</span> <span class="o">=</span> <span class="mi">100</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="n">n_data</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">n_data</span><span class="p">)</span>
<span class="n">my_pwlf_gen</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">true_beta</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="n">true_breaks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.75</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">])</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">my_pwlf_gen</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span><span class="n">true_beta</span><span class="p">,</span> <span class="n">breaks</span><span class="o">=</span><span class="n">true_breaks</span><span class="p">)</span>

<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;True piecewise linear data&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default" id="id8">
<img alt="True piecewise linear data." src="https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/true_pwlf.png" />
<p class="caption"><span class="caption-text">True piecewise linear data.</span><a class="headerlink" href="#id8" title="Permalink to this image">¶</a></p>
</div>
<p>Now we can perform a fit, calculate the standard errors, and p-values.
The non-linear method uses a first order taylor series expansion to
linearize the non-linear regression problem. A positive step_size
performs a forward difference, and a negative step_size would perform a
backwards difference.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">my_pwlf</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fitfast</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">pop</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">p_values</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&#39;non-linear&#39;</span><span class="p">,</span> <span class="n">step_size</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">)</span>
<span class="n">se</span> <span class="o">=</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">se</span>  <span class="c1"># standard errors</span>
</pre></div>
</div>
<p>The standard errors and p-values correspond to each model parameter.
First the beta parameters (intercept + slopes) and then the breakpoints.
We can assemble the parameters, and print a table of the result with the
following code.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">parameters</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">my_pwlf</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span>
                             <span class="n">my_pwlf</span><span class="o">.</span><span class="n">fit_breaks</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>

<span class="n">header</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Parameter type&#39;</span><span class="p">,</span> <span class="s1">&#39;Parameter value&#39;</span><span class="p">,</span> <span class="s1">&#39;Standard error&#39;</span><span class="p">,</span> <span class="s1">&#39;t&#39;</span><span class="p">,</span>
          <span class="s1">&#39;P &gt; np.abs(t) (p-value)&#39;</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="o">*</span><span class="n">header</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&#39; | &#39;</span><span class="p">)</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">parameters</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">object_</span><span class="p">)</span>
<span class="n">values</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">values</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">se</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">values</span><span class="p">[:,</span> <span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">parameters</span> <span class="o">/</span> <span class="n">se</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">values</span><span class="p">[:,</span> <span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">my_pwlf</span><span class="o">.</span><span class="n">beta</span><span class="o">.</span><span class="n">size</span><span class="p">:</span>
        <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Beta&#39;</span>
        <span class="k">print</span><span class="p">(</span><span class="o">*</span><span class="n">row</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&#39; | &#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Breakpoint&#39;</span>
        <span class="k">print</span><span class="p">(</span><span class="o">*</span><span class="n">row</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&#39; | &#39;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils align-default">
<colgroup>
<col style="width: 18%" />
<col style="width: 19%" />
<col style="width: 18%" />
<col style="width: 18%" />
<col style="width: 29%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Parameter type</p></th>
<th class="head"><p>Parameter value</p></th>
<th class="head"><p>Standard error</p></th>
<th class="head"><p>t</p></th>
<th class="head"><p>P &gt; np.abs(t) (p-value)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Beta</p></td>
<td><p>1.821</p></td>
<td><p>0.0</p></td>
<td><p>1763191476.046</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-odd"><td><p>Beta</p></td>
<td><p>-0.427</p></td>
<td><p>0.0</p></td>
<td><p>-46404554.493</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-even"><td><p>Beta</p></td>
<td><p>-1.165</p></td>
<td><p>0.0</p></td>
<td><p>-111181494.162</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-odd"><td><p>Beta</p></td>
<td><p>-1.397</p></td>
<td><p>0.0</p></td>
<td><p>-168954500.421</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-even"><td><p>Beta</p></td>
<td><p>0.873</p></td>
<td><p>0.0</p></td>
<td><p>93753841.242</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-odd"><td><p>Breakpoint</p></td>
<td><p>0.2</p></td>
<td><p>0.0</p></td>
<td><p>166901856.885</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-even"><td><p>Breakpoint</p></td>
<td><p>0.5</p></td>
<td><p>0.0</p></td>
<td><p>537785803.646</p></td>
<td><p>0.0</p></td>
</tr>
<tr class="row-odd"><td><p>Breakpoint</p></td>
<td><p>0.75</p></td>
<td><p>0.0</p></td>
<td><p>482311769.159</p></td>
<td><p>0.0</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="obtain-the-equations-of-fitted-pwlf">
<h2>obtain the equations of fitted pwlf<a class="headerlink" href="#obtain-the-equations-of-fitted-pwlf" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you may want the mathematical equations that represent your
fitted model. This is easy to perform if you don’t mind using sympy.</p>
<p>The following code will fit 5 line segments of degree=2 to a sin wave.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pwlf</span>
<span class="c1"># generate sin wave data</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># add noise to the data</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span> <span class="o">+</span> <span class="n">y</span>
<span class="n">my_pwlf_2</span> <span class="o">=</span> <span class="n">pwlf</span><span class="o">.</span><span class="n">PiecewiseLinFit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">degree</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">res2</span> <span class="o">=</span> <span class="n">my_pwlf_2</span><span class="o">.</span><span class="n">fitfast</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">pop</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
</pre></div>
</div>
<p>Given this fit, the following code will print the mathematical equation
for each line segment.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="kn">from</span> <span class="nn">sympy.utilities</span> <span class="kn">import</span> <span class="n">lambdify</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">get_symbolic_eqn</span><span class="p">(</span><span class="n">pwlf_</span><span class="p">,</span> <span class="n">segment_number</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">pwlf_</span><span class="o">.</span><span class="n">degree</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Degree must be at least 1&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">segment_number</span> <span class="o">&lt;</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">segment_number</span> <span class="o">&gt;</span> <span class="n">pwlf_</span><span class="o">.</span><span class="n">n_segments</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;segment_number not possible&#39;</span><span class="p">)</span>
    <span class="c1"># assemble degree = 1 first</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">segment_number</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">my_eqn</span> <span class="o">=</span> <span class="n">pwlf_</span><span class="o">.</span><span class="n">beta</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">pwlf_</span><span class="o">.</span><span class="n">beta</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">pwlf_</span><span class="o">.</span><span class="n">fit_breaks</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">my_eqn</span> <span class="o">+=</span> <span class="p">(</span><span class="n">pwlf_</span><span class="o">.</span><span class="n">beta</span><span class="p">[</span><span class="n">line</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">pwlf_</span><span class="o">.</span><span class="n">fit_breaks</span><span class="p">[</span><span class="n">line</span><span class="p">])</span>
    <span class="c1"># assemble all other degrees</span>
    <span class="k">if</span> <span class="n">pwlf_</span><span class="o">.</span><span class="n">degree</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">pwlf_</span><span class="o">.</span><span class="n">degree</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">segment_number</span><span class="p">):</span>
                <span class="n">beta_index</span> <span class="o">=</span> <span class="n">pwlf_</span><span class="o">.</span><span class="n">n_segments</span><span class="o">*</span><span class="p">(</span><span class="n">k</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">line</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">my_eqn</span> <span class="o">+=</span> <span class="p">(</span><span class="n">pwlf_</span><span class="o">.</span><span class="n">beta</span><span class="p">[</span><span class="n">beta_index</span><span class="p">])</span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">pwlf_</span><span class="o">.</span><span class="n">fit_breaks</span><span class="p">[</span><span class="n">line</span><span class="p">])</span><span class="o">**</span><span class="n">k</span>
    <span class="k">return</span> <span class="n">my_eqn</span><span class="o">.</span><span class="n">simplify</span><span class="p">()</span>


<span class="n">eqn_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">f_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">my_pwlf_2</span><span class="o">.</span><span class="n">n_segments</span><span class="p">):</span>
    <span class="n">eqn_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">get_symbolic_eqn</span><span class="p">(</span><span class="n">my_pwlf_2</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
    <span class="k">print</span><span class="p">(</span><span class="s1">&#39;Equation number: &#39;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="n">eqn_list</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
    <span class="n">f_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">eqn_list</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
</pre></div>
</div>
<p>which should print out something like the following:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">Equation</span> <span class="n">number</span><span class="p">:</span>  <span class="mi">1</span>
<span class="o">-</span><span class="mf">0.953964059782599</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">1.89945177490653</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mf">0.00538634182565454</span>
<span class="n">Equation</span> <span class="n">number</span><span class="p">:</span>  <span class="mi">2</span>
<span class="mf">0.951561315686298</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mf">5.69747505830914</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mf">7.5772216545711</span>
<span class="n">Equation</span> <span class="n">number</span><span class="p">:</span>  <span class="mi">3</span>
<span class="o">-</span><span class="mf">0.949735350431857</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">9.48218236957122</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mf">22.720785454735</span>
<span class="n">Equation</span> <span class="n">number</span><span class="p">:</span>  <span class="mi">4</span>
<span class="mf">0.926850298824217</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mf">12.9824424358344</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mf">44.5102742956827</span>
<span class="n">Equation</span> <span class="n">number</span><span class="p">:</span>  <span class="mi">5</span>
<span class="o">-</span><span class="mf">1.03016230425747</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">18.5306546317065</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mf">82.3508513333073</span>
</pre></div>
</div>
<p>For more information on how this works, see
<a class="reference external" href="https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/understanding_higher_degrees/polynomials_in_pwlf.ipynb">this</a>
jupyter notebook.</p>
</div>
</div>


          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">pwlf</a></h1>








<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="how_it_works.html">How it works</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Examples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#fit-with-known-breakpoint-locations">fit with known breakpoint locations</a></li>
<li class="toctree-l2"><a class="reference internal" href="#fit-for-specified-number-of-line-segments">fit for specified number of line segments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#fitfast-for-specified-number-of-line-segments">fitfast for specified number of line segments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#force-a-fit-through-data-points">force a fit through data points</a></li>
<li class="toctree-l2"><a class="reference internal" href="#use-custom-optimization-routine">use custom optimization routine</a></li>
<li class="toctree-l2"><a class="reference internal" href="#pass-differential-evolution-keywords">pass differential evolution keywords</a></li>
<li class="toctree-l2"><a class="reference internal" href="#find-the-best-number-of-line-segments">find the best number of line segments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#model-persistence">model persistence</a></li>
<li class="toctree-l2"><a class="reference internal" href="#bad-fits-when-you-have-more-unknowns-than-data">bad fits when you have more unknowns than data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#fit-with-a-breakpoint-guess">fit with a breakpoint guess</a></li>
<li class="toctree-l2"><a class="reference internal" href="#get-the-linear-regression-matrix">get the linear regression matrix</a></li>
<li class="toctree-l2"><a class="reference internal" href="#use-of-tensorflow">use of tensorflow</a></li>
<li class="toctree-l2"><a class="reference internal" href="#fit-constants-or-polynomials">fit constants or polynomials</a></li>
<li class="toctree-l2"><a class="reference internal" href="#specify-breakpoint-bounds">specify breakpoint bounds</a></li>
<li class="toctree-l2"><a class="reference internal" href="#non-linear-standard-errors-and-p-values">non-linear standard errors and p-values</a></li>
<li class="toctree-l2"><a class="reference internal" href="#obtain-the-equations-of-fitted-pwlf">obtain the equations of fitted pwlf</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="pwlf.html">pwlf package contents</a></li>
<li class="toctree-l1"><a class="reference internal" href="about.html">About</a></li>
<li class="toctree-l1"><a class="reference internal" href="requirements.html">Requirements</a></li>
<li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="how_it_works.html" title="previous chapter">How it works</a></li>
      <li>Next: <a href="pwlf.html" title="next chapter">pwlf package contents</a></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Charles Jekel.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 2.3.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
      |
      <a href="_sources/examples.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-67542734-2']);
      _gaq.push(['_setDomainName', 'none']);
      _gaq.push(['_setAllowLinker', true]);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
    
  </body>
</html>